Skip to content

Fix invalid child index error when undoing deletion of multiple nodes - #125904

Open
SaswatJK wants to merge 1 commit into
godotengine:masterfrom
SaswatJK:fix-undo-delete-child-index
Open

SaswatJK wants to merge 1 commit into
godotengine:masterfrom
SaswatJK:fix-undo-delete-child-index

Conversation

@SaswatJK

Copy link
Copy Markdown

Undo for deleted nodes were recorded in selection order, without sorting the nodes into tree order. Undo then restored siblings out of order, so move_child could be called with an index that didn't exist yet.

PR description
AI disclosure: Double-checked by asking claude chat whether the fix would have side-effects or no. Also asked it to judge this PR.

After deleting multiple sibling nodes selected in a non-top-to-bottom order (Selecting in the 2D viewport through mouse-hold, or holding ctrl and clicking bottom-up in the Scene dock), undoing the deletions prints ERROR: Invalid new child index: (previous index number).

This happens because in SceneTreeDock::_delete_confirm, the undo methods for each deleted node are restored one by one in the order of the selection, so a node can be restored before its siblings, at a point where its original index doesn't exist yet.

For whatever reason, the selection was being sorted with Node::Comparator only for the animation track cleanup after the undo methods had already been recorded.

This PR moves the same sort before recording, so that undo always restores nodes in ascending index order. This means that the animation track cleanup will work as it did before.

Tested with the MRP from the issue:

  • Box select the four Icon nodes, delete, undo: no error, nodes restored in original order.
  • Ctrl-click nodes bottom-up in the Scene dock, delete, undo
  • Cut (Ctrl+X) + undo
  • Redo after undo

Reproducible since at least 4.6.3 according to the issue (haven't checked myself), so not a recent regression.

Undo for deleted nodes were recorded in selection order, witout
sorting the nodes into tree order. Undo then restored siblings out of
order, so `move_child` could be called with an index that didn't exist yet.

PR description
AI disclosure: Asked Claude whether the fix would have side-effects or no. Also asked it to judge this PR.

After deleting multiple sibling nodes selected in a non-top-to-bottom order (Selecting in the 2D viewport through mouse-hodl, or holding ctrl and clicking bottom-up in the Scene dock), undoing the deletions prints `ERROR: Invalid new child index: (previous index number).`

This happens because in `SceneTreeDock::_delete_confirm`, the undo methods for each deleted node are restored one by one in the order of the selection, so a node can be restored before its siblings, at a point where its original index doesn't exist yet.

For whatever reason, the selection was being sorted with `Node::Comparator` only for the animation track cleanup after the undo methods had already been recorded.

This PR moves the same sort before recording, so that undo always restores nodes in ascending index order. This means that the animation track cleanup will work as it did before.

- Closes godotengine#125903

Tested with the MRP from the issue:
- Box select the four Icon nodes, delete, undo: no error, nodes restored in original order.
- Ctrl-click nodes bottom-up in the Scene dock, delete, undo
- Cut (Ctrl+X) + undo
- Redo after undo

Reproducible since at least 4.6.3 according to the issue (haven't checked myself), so not a recent regression.
@SaswatJK
SaswatJK requested a review from a team as a code owner November 28, 2026 16:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ERROR: Invalid new child index 2, when undoing the deletion of 3+ nodes, selected in a non-top-to-bottom order

1 participant